package com.example.project.service.impl;

import com.example.project.base.BaseServiceImpl;
import com.example.project.entity.TRole;
import com.example.project.entity.TRole_Module;
import com.example.project.mapper.TRoleMapper;
import com.example.project.mapper.TRoleModuleMapper;
import com.example.project.service.TRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

@Service
public class TRoleServiceImpl extends BaseServiceImpl<TRole> implements TRoleService {

    @Autowired
    TRoleModuleMapper tRoleModuleMapper;
    @Autowired
    TRoleMapper tRoleMapper;

    public Integer giveModuleByRole(Integer roleId, Integer moduleId){
        int num=tRoleModuleMapper.insert(new TRole_Module(){{
            setModuleId(moduleId);
            setRoleId(roleId);
        }});
        return num;
    }

    public Integer takeBackModuleByRole(Integer roleId, Integer moduleId){

        Example example = new Example(TRole_Module.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("roleId",roleId);
        criteria.andEqualTo("moduleId",moduleId);

        int num=tRoleModuleMapper.deleteByExample(example);

        return num;
    }

    public Integer addRole(TRole tRole){

        Integer maxSort=super.selectMaxSort();
        tRole.setSort(maxSort);
        tRole.setRoleCode("admin"+tRole.getSort());
        tRole.setIsRoot(0);

        return tRoleMapper.insert(tRole);
    }
    public Integer updateRole(TRole tRole){
        return tRoleMapper.updateRole(tRole);
    }
}
